package net.csdn.business.discuss.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.csdn.business.discuss.api.mapper.DiscussIdMapper;
import net.csdn.business.discuss.api.model.entity.DiscussId;
import net.csdn.business.discuss.api.service.IDiscussIdService;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author likun
 * @date 2022/6/16 20:15
 */
@Component
public class DiscussIdServiceImpl extends ServiceImpl<DiscussIdMapper, DiscussId> implements IDiscussIdService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer getMaxId(String sourceId,Integer sourceType) {
        DiscussId discussId=DiscussId.builder().sourceId(sourceId).sourceType(sourceType).build();
        baseMapper.insertDiscussId(discussId);
        DiscussId idObject = findBySourceId(discussId.getSourceId());
        return idObject.getMaxId();
    }

    @Override
    public DiscussId findBySourceId(String sourceId) {
        QueryWrapper<DiscussId> query = new QueryWrapper<>();
        query.lambda().eq(DiscussId::getSourceId, sourceId);
        return baseMapper.selectOne(query);
    }
}
